area 	= null;
colors 	= null;
sizes 	= null;
mode 	= 0;

function insertAtCursorPosition(content) {
    if(document.selection) {
        area.focus();
        range = document.selection.createRange();
        range.text = content;
        range.select();
    } else if(area.selectionStart || area.selectionStart == '0') {
        posStart = area.selectionStart;
        posEnd = area.selectionEnd;
        area.value = area.value.substring(0, posStart) + content + area.value.substring(posEnd, area.value.length);
        area.focus();
        area.selectionStart = area.selectionEnd = posStart + content.length;
    } else {
        area.value += content;
        area.focus();
    }
}

function addBBCode(startTag, endTag, content, bbcode) {
    switch(mode) {
        case 0:
            insertAtCursorPosition('[' + startTag + ']' + content + '[/' + endTag + ']');
        break;

        case 1:
            bbcode = document.getElementById(bbcode);
            if(content == 'start') {
                newValue = '[' + startTag + ']';
                bbcode.innerHTML = bbcode.innerHTML + '*';
            } else if(content == 'end') {
                newValue = '[/' + endTag + ']';
                bbcode.innerHTML = bbcode.innerHTML.replace(/\*/, '');
            }
            insertAtCursorPosition(newValue);
        break;
    }
}

function BBCodeAction(type) {
    switch(type) {
        case "bold":
            promptString = "Gebe den Text ein, der Fett dargestellt werden soll:";
            startTag = endTag = 'b';
        break;

        case "italic":
            promptString = "Gebe den Text ein, der Kursiv dargestellt werden soll:";
            startTag = endTag = 'i';
        break;

        case "underline":
            promptString = "Gebe den Text ein, der unterstrichen dargestellt werden soll:";
            startTag = endTag = 'u';
        break;

        case "img":
            promptString = "Gebe die URL zu dem Bild ein:";
            startTag = endTag = 'img';
        break;

        case "url":
            promptString = "Gebe die URL ein:";
            startTag = endTag = 'url';
        break;

        case "code":
            promptString = "Gebe deinen Code ein:";
            startTag = endTag = 'code';
        break;

        case "left":
            promptString = "Gebe den Text ein, der Linksbündig angezeigt werden soll:";
            startTag = endTag = 'left';
        break;

        case "right":
            promptString = "Gebe den Text ein, der Rechtsbündig angezeigt werden soll:";
            startTag = endTag = 'right';
        break;

        case "color":
            promptString = "Gebe den Text ein, der " + colors.options[colors.selectedIndex].innerHTML + " dargestellt werden soll:";
            startTag = 'color=' + colors.options[colors.selectedIndex].value;
            endTag = 'color';
        break;

        case "size":
            promptString = "Gebe den Text ein, der in der Schriftgröße " + sizes.options[sizes.selectedIndex].innerHTML + " dargestellt werden soll:";
            startTag = 'size=' + sizes.options[sizes.selectedIndex].value;
            endTag = 'size';
        break;
    }

    if(typeof area.selectionStart != 'undefined' && area.value.substring(area.selectionStart, area.selectionEnd).length != 0) {
        selection = area.value.substring(area.selectionStart, area.selectionEnd);
        insertAtCursorPosition('[' + startTag + ']' + selection + '[/' + endTag + ']');
    } else if(typeof document.selection != 'undefined' && document.selection.createRange().text.length != 0)
        insertAtCursorPosition('[' + startTag + ']' + document.selection.createRange().text + '[/' + endTag + ']');
    else if(!mode || type == 'color' || type == 'size') {
        userInput = prompt(promptString, '');
        if(userInput != null)
            addBBCode(startTag, endTag, userInput, '');
    } else if(mode)
        addBBCode(startTag, endTag, (document.getElementById(type).innerHTML.match(/\*/)?'end':'start'), type);
}

function BBCodes(textareaID) {
    area = document.getElementById(textareaID);
    colors = document.getElementById('color');
    sizes = document.getElementById('size');
    links = document.getElementsByTagName('a');
    pics = document.getElementsByTagName('img');
    for(i = 0;i < links.length;i++) {
        link = links[i];
        if(link.getAttribute('rel') == 'bbcode') {
            link.onclick = function() {
                BBCodeAction(this.id);
                return false;
            }
        }
    }
    for(n = 0;n < pics.length;n++) {
        pic = pics[n];
        if(pic.className == 'smilie') {
            pic.onclick = function() {
                insertAtCursorPosition(this.name);
            }
        }
    }
    colors.onchange = function() {
        BBCodeAction('color');
    }
    sizes.onchange = function() {
        BBCodeAction('size');
    }
}

function changeMode() {
    document.getElementById('mode0').onclick = function() {
        mode = 0;
    }

    document.getElementById('mode1').onclick = function() {
        mode = 1;
    }
}

window.onload = function() {
    BBCodes('text');
    changeMode();
}

function popup(height, width, url, desc) {
	popup = window.open(url, desc, "width="+width+",height="+height+",status=yes,scrollbars=yes,resizable=yes");
	popup.focus();
}

function quote(num) {
	var input = document.getElementById('text');
  	input.focus();
  	
  	var current_value	= input.value;
  	
  	input.value = current_value + "\n[quote]"+num+"[/quote]\n";
}

function toggle(val) {
	if ($("#row1"+val).is(":hidden")) 
	{
		$("#row1"+val).fadeIn("slow");
		$("#row2"+val).fadeIn("slow");
	} 
	else 
	{
		$("#row1"+val).fadeOut("slow");
		$("#row2"+val).fadeOut("slow");
	}
}